/**
 * 
 */
package com.jzweb.springbootmail.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 030998
 *
 */
@Controller
@RequestMapping("/down")
public class DownFileController {

	@RequestMapping("/df")
	public void down(HttpServletResponse response) throws IOException {
		String zipName = "myfile.zip";
        response.setContentType("multipart/form-data");  
        response.setHeader("Content-Disposition","attachment;filename="+zipName);
        ZipOutputStream zos = null;
        OutputStream os = null;
        try {
        	os = response.getOutputStream();
        	zos = new ZipOutputStream(os);
        	downAct(zos);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(zos != null) {
				zos.close();
			}
			if(os != null) {
				os.close();
				os = null;
			}
		}
        
        
	}
	
	
	private void downAct(ZipOutputStream zos) {
		List<String> list = new ArrayList<String>();
        list.add("C:\\Users\\030998\\Desktop\\ERP移动办公部分一级主管的.xlsx");
        list.add("C:\\Users\\030998\\Desktop\\公司领导审批流程2021-9-7.xlsx");
        list.add("C:\\Users\\030998\\Desktop\\Android-VPN-SDK-20210511(1).zip");
        list.add("C:\\Users\\030998\\Desktop\\ArrayVPNSDK_20210203(1).zip");
        
        
        
        for(int i=0;i<list.size();i++) {
        	File file = new File(list.get(i));
        	zipFile(zos, file);
        }
	}
	
	
	private void zipFile(ZipOutputStream zos, File file) {
    	//InputStream is = null;
        BufferedInputStream in = null;
        ZipEntry entry = new ZipEntry(file.getName());
        try {
        	byte[] buffer = new byte[1024];
            int len;
            FileInputStream filein = new FileInputStream(file);
            in = new BufferedInputStream(filein);
            zos.putNextEntry(entry);
            while((len = in.read(buffer)) != -1) {
            	zos.write(buffer, 0, len);
            }
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(entry != null) {
				try {
					zos.closeEntry();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if(in != null) {
				try {
					in.close();
				} catch (Exception e3) {
					e3.printStackTrace();
				}
			}
			
		}
	}
}
